﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.Models.View
{
    /// <summary>
    /// 操作员年资金情况
    /// </summary>
    public class OperatorYearMoney
    {
        public int Id { get; set; }
        /// <summary>
        /// 操作员编号
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        public int Year { get; set; }

        /// <summary>
        /// 多仓持仓
        /// </summary>
        public int LongPosition { get; set; }
        /// <summary>
        /// 空仓持仓
        /// </summary>
        public int ShortPosition { get; set; }
        /// <summary>
        /// 期初结存
        /// </summary>
        public double PresetTlePosit { get; set; }
        /// <summary>
        /// 期末结存
        /// </summary>
        public double SettlePosit { get; set; }
        /// <summary>
        /// 冻结保证金
        /// </summary>
        public double FrozenMargin { get; set; }
        /// <summary>
        /// 冻结手续费
        /// </summary>
        public double FrozenCommission { get; set; }
        /// <summary>
        /// 入金金额
        /// </summary>
        public double Deposit { get; set; }
        /// <summary>
        /// 出金金额
        /// </summary>
        public double WithDraw { get; set; }
        /// <summary>
        /// 交割保证金
        /// </summary>
        public double DeliveryMargin { get; set; }
        /// <summary>
        /// 交割手续费
        /// </summary>
        public double DeliveryCommission { get; set; }
        /// <summary>
        /// 追加保证金
        /// </summary>
        public double AppendMargin { get; set; }
        /// <summary>
        /// 基础保证金
        /// </summary>
        public double BaseMargin { get; set; }
        /// <summary>
        /// 质押金额
        /// </summary>
        public double Mortgage { get; set; }
        /// <summary>
        /// 上次质押金额
        /// </summary>
        public double Premortgage { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Commission { get; set; }
        /// <summary>
        /// 可用资金
        /// </summary>
        public double AvailableMoney { get; set; }
        /// <summary>
        /// 可取资金
        /// </summary>
        public double WithDrawQuota { get; set; }
        /// <summary>
        /// 动态权益
        /// </summary>
        public double DynamicMoney { get; set; }
        /// <summary>
        /// 占用保证金
        /// </summary>
        public double Margin { get; set; }
        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double CloseProfit { get; set; }
        /// <summary>
        /// 持仓盈亏
        /// </summary>
        public double PositionProfit { get; set; }
        /// <summary>
        /// 风险度
        /// </summary>
        public double RiskRate { get; set; }

        ///// <summary>
        ///// 得到交易日账户信息
        ///// </summary>
        ///// <param name="accountMoneys"></param>
        ///// <returns></returns>
        //public OperatorYearMoney GetOperatorYearMoney(int year, int operatorId, DB.DBManager db)
        //{
        //    var query = db.OperatorYearMoney.Where(a => a.OperatorId == operatorId).Where(a => a.Year == year);
        //    if (query.Count() > 0)
        //    {
        //        return query.First();
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}


        ///// <summary>
        ///// 初始化操作员资金信息
        ///// </summary>
        ///// <param name="tradeHistoryId"></param>
        ///// <param name="db"></param>
        //public void InitOperatorYearMoney(int year, DB.DBManager db)
        //{
        //    var operators = db.Operators.ToList();
        //    View.OperatorYearMoney money = new OperatorYearMoney();
        //    foreach (Sys.Operator op in operators)
        //    {
        //        var moneys = db.OperatorMonthMoney.Where(a => a.Year == year).Where(a => a.OperatorId == op.Id).ToList();
        //        var queryExist = db.OperatorYearMoney.Where(a => a.OperatorId == op.Id).Where(a => a.Year == year).ToList();
        //        if (queryExist.Count() > 0)
        //        {
        //            queryExist.First().AppendMargin = moneys.Sum(a => a.AppendMargin);
        //            queryExist.First().AvailableMoney = moneys.Sum(a => a.AvailableMoney);
        //            queryExist.First().BaseMargin = moneys.Sum(a => a.BaseMargin);
        //            queryExist.First().CloseProfit = moneys.Sum(a => a.CloseProfit);
        //            queryExist.First().Commission = moneys.Sum(a => a.Commission);
        //            queryExist.First().DeliveryCommission = moneys.Sum(a => a.DeliveryMargin);
        //            queryExist.First().Deposit = moneys.Sum(a => a.Deposit);
        //            queryExist.First().DynamicMoney = moneys.Sum(a => a.DynamicMoney);
        //            queryExist.First().FrozenCommission = moneys.Sum(a => a.FrozenCommission);
        //            queryExist.First().FrozenMargin = moneys.Sum(a => a.FrozenMargin);
        //            queryExist.First().LongPosition = moneys.Sum(a => a.LongPosition);
        //            queryExist.First().Margin = moneys.Sum(a => a.Margin);
        //            queryExist.First().Mortgage = moneys.Sum(a => a.Mortgage);
        //            queryExist.First().PositionProfit = moneys.Sum(a => a.PositionProfit);
        //            queryExist.First().Premortgage = moneys.Sum(a => a.Premortgage);
        //            queryExist.First().PresetTlePosit = moneys.Sum(a => a.PresetTlePosit);
        //            queryExist.First().RiskRate = moneys.Sum(a => a.RiskRate);
        //            queryExist.First().SettlePosit = moneys.Sum(a => a.SettlePosit);
        //            queryExist.First().ShortPosition = moneys.Sum(a => a.ShortPosition);
        //            queryExist.First().WithDraw = moneys.Sum(a => a.WithDraw);
        //            queryExist.First().WithDrawQuota = moneys.Sum(a => a.WithDrawQuota);
        //            queryExist.First().Year = year;
        //            queryExist.First().OperatorId = op.Id;
        //        }
        //        else
        //        {
        //            View.OperatorYearMoney oym = new View.OperatorYearMoney();
        //            oym.Year = year;
        //            oym.OperatorId = op.Id;
        //            oym.AppendMargin = moneys.Sum(a => a.AppendMargin);
        //            oym.AvailableMoney = moneys.Sum(a => a.AvailableMoney);
        //            oym.BaseMargin = moneys.Sum(a => a.BaseMargin);
        //            oym.CloseProfit = moneys.Sum(a => a.CloseProfit);
        //            oym.Commission = moneys.Sum(a => a.Commission);
        //            oym.DeliveryCommission = moneys.Sum(a => a.DeliveryMargin);
        //            oym.Deposit = moneys.Sum(a => a.Deposit);
        //            oym.DynamicMoney = moneys.Sum(a => a.DynamicMoney);
        //            oym.FrozenCommission = moneys.Sum(a => a.FrozenCommission);
        //            oym.FrozenMargin = moneys.Sum(a => a.FrozenMargin);
        //            oym.LongPosition = moneys.Sum(a => a.LongPosition);
        //            oym.Margin = moneys.Sum(a => a.Margin);
        //            oym.Mortgage = moneys.Sum(a => a.Mortgage);
        //            oym.PositionProfit = moneys.Sum(a => a.PositionProfit);
        //            oym.Premortgage = moneys.Sum(a => a.Premortgage);
        //            oym.PresetTlePosit = moneys.Sum(a => a.PresetTlePosit);
        //            oym.RiskRate = moneys.Sum(a => a.RiskRate);
        //            oym.SettlePosit = moneys.Sum(a => a.SettlePosit);
        //            oym.ShortPosition = moneys.Sum(a => a.ShortPosition);
        //            oym.WithDraw = moneys.Sum(a => a.WithDraw);
        //            oym.WithDrawQuota = moneys.Sum(a => a.WithDrawQuota);
        //            oym.Year = year;
        //            oym.OperatorId = op.Id;
        //            db.OperatorYearMoney.Add(oym);
        //        }
        //    }
        //    db.SaveChanges();
        //}

        //public void Delete(int year, DB.DBManager db)
        //{
        //    var query = db.OperatorYearMoney.Where(a => a.Year == year);
        //    foreach (OperatorYearMoney m in query)
        //    {
        //        db.OperatorYearMoney.Remove(m);
        //    }
        //    db.SaveChanges();
        //}
    }
}
